<?php 
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 路线控制器
 * 
 * @author 	FangMinglong
 * @date	2017年5月17日 下午5:05:24
 *
 * @desc    完善文档注释
 * @version 1.1.0
 * @date	2017年6月27日 下午5:41:36
 *
 * @title 路线控制器-路线相关功能
 * @entry http://discovery.segway.com
 */
class Touroute extends MY_Consumer_Controller
{
	// Must be public!!!
	/** @var Lib_touroute */
	public $lib_touroute = null;
	
	public function __construct()
	{
		parent::__construct();
		
		$this->load->library('lib_touroute');
		
		$this->lang->load('touroute');
	}
	
	/**
	 * 路线详情页
	 *
	 * @desc    
	 * @author  Jason
	 * @date	2017年5月17日 下午5:08:03
	 * @param   number $id 路线ID
	 * 			把参数按顺序辍到 URL 之后，用 "/" 间隔
	 * @example /touroute/detail/45
	 * 
	 * @link    /touroute/detail
	 * 
	 * @demoParam id 45
	 */
	public function detail($id)
	{
		$cookie_lang = $this->lib_cookie->get_language();
		
		$language = $this->lib_language->get_supported_language($this->language, $cookie_lang);
		
		$currency = $this->lib_cookie->get_currency();
		
		$touroute = $this->lib_touroute->get_touroute_for_detail($id, $language, $currency);
		
		$this->set_mutable_view_var('head.title', $touroute['name']);
		$this->set_mutable_view_var('head.keywords', $touroute['summary']);
		$this->set_mutable_view_var('head.description', $touroute['summary']);
		
		$data['content']['touroute'] = $touroute;
		
		$this->load->library('lib_merchant');
		$merchant_title = $this->lib_merchant->get_merchant_as_title($touroute['merchant_id'], $language);
		$data['content']['merchant'] = $merchant_title;
		
		$this->load->view('consumer/touroute/detail', $data);
	}
	
	/**
	 * 路线搜索页
	 *
	 * @desc	搜索出来的全是路线
	 * @author  Jason
	 * @date	2017年5月18日 下午12:52:12
	 * @param   string $keyword 关键字
	 * @param   string $order   排序关键字，duration_desc, duration_asc, price_desc, price_asc, default。可省略，默认浏览量排列。
	 * @param   number $country 国家ID，默认为 0
	 * @param   number $city    城市ID，默认为 0
	 * @param   string $language 默认为 "english"
	 * 			参数以 QUERY STRING 的方式传递！
	 * @example 如果参数为 keyword=segwa order=default country=693000 city=86690000
	 * 			那么URL为 /touroute/search/?keyword=segwa&order=default&country=693000&city=86690000
	 * 
	 * @link    /touroute/search
	 * 
	 * @actionParamsUriProtocolRule query_string
	 * @demoParam keyword segwa
	 * @demoParam order default
	 * @demoParam country 693000
	 * @demoParam city 86690000
	 * 
	 * @desc    放开关键字为空的限制
	 * @version 1.1.0
	 * @date	2017年6月28日 下午5:18:12
	 * 
	 * @TODO	使用 ConditionParser.get_condition 方法引入查询条件
	 * @date	2017年7月17日 下午9:41:07
	 */
	public function search(/* $keyword, $order = 'default', $country = 0, $city = 0, $language = '' */)
	{
		// 先接参数
		$keyword = $this->input->get('keyword') 	OR $keyword = '';
		$order = $this->input->get('order') 		OR $order = 'default';
		$country = $this->input->get('country') 	OR $country = 0;
		$city = $this->input->get('city') 			OR $city = 0;
		$language = $this->input->get('language') 	OR $language = '';
		$page = $this->input->get('page') 			OR $page = 1;
		$number = $this->input->get('number') 		OR $number = 6;
		
		$cookie_lang = $this->lib_cookie->get_language();
		
		$language = $this->lib_language->get_supported_language($language, $cookie_lang);
		
		$currency = $this->lib_cookie->get_currency();
		
		$this->load->library('lib_touroute');
		$resources = $this->lib_touroute->get_ids_for_autocomplete($country, $city);
		$resources[] = -1;
		
		$module = 'touroute';
		$search_by = ['name'];
		$resources = $this->lib_language->get_resources_for_search($keyword, $module, $resources, $language, $search_by);
		$resources[] = -1;
		
		$conditions = [['where_in', ['id', $resources]]];
		switch ($order) {
			//duration_desc, duration_asc, price_desc, price_asc
			case 'duration_desc':
				$conditions[] = ['order_by', 'duration DESC']; break;
			case 'duration_asc':
				$conditions[] = ['order_by', 'duration ASC']; break;
			case 'price_desc':
				$conditions[] = ['order_by', 'channel_amount DESC']; break;
			case 'price_asc':
				$conditions[] = ['order_by', 'channel_amount ASC']; break;
			default:
				$conditions[] = ['order_by', 'page_view DESC']; break;
		}
		$touroutes = $this->lib_touroute->get_touroutes_for_list($conditions, $language, $page, $number);
		
		$this->load->library('lib_merchant');
		$this->lib_merchant->append_merchant_official($touroutes['data'], ['is_official']);
		
// 		$this->set_mutable_view_var('head.title', $touroute['name']);
// 		$this->set_mutable_view_var('head.keywords', $touroute['summary']);
// 		$this->set_mutable_view_var('head.description', $touroute['summary']);
		
		$this->load->library('lib_area');
		list($status, $countries) = $this->lib_area->list_children(0, $language);
		if ($country){
			list($status, $cities) = $this->lib_area->list_children($country, $language);
		}else{
			$cities = [];
		}
		$metrix = [
				'country' => $countries,
				'city' => $cities,
		];
		
		$data['content']['touroutes'] = $touroutes;
		$data['content']['areas'] = $metrix;
		
		$this->load->view('consumer/touroute/search', $data);
	}
	

}